package cn.itcast.wushangxiantu.Activity;


import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;

import cn.itcast.wushangxiantu.BaseAdapter.BagGriveViewAdapter;
import cn.itcast.wushangxiantu.UserData.UserBag;
import cn.itcast.wushangxiantu.MessageEvent;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Bag extends AppCompatActivity implements View.OnClickListener {

    //声明
    private GridView gvBag;
    private TextView tvZhuangbei;
    private TextView tvDaoju;
    private TextView tvLingshi;

    private UserBag userBag;
    private AnimationDrawable animationDrawable;
    private BagGriveViewAdapter bagGriveViewAdapter;
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bag);
        //注册
        EventBus.getDefault().register(this);
        initView();

    }

    private void initView() {
        tvZhuangbei = (TextView) findViewById(R.id.tvZhuangbei);
        tvDaoju = (TextView) findViewById(R.id.tvDaoju);
        tvLingshi = (TextView) findViewById(R.id.tvLingshi);
        gvBag = (GridView) findViewById(R.id.gvBag);
        iv = (ImageView) findViewById(R.id.iv);

        if (animationDrawable == null) {
            animationDrawable = (AnimationDrawable) iv.getBackground();
        }
        if (!animationDrawable.isRunning()){
            animationDrawable.start();
        }else {
            iv.setVisibility(View.INVISIBLE);
            animationDrawable.stop();
        }
        getBagData(Login.getUser().getUsernumber());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvZhuangbei:
                changeMenu(0);
                break;
            case R.id.tvDaoju:
                changeMenu(1);
                break;
            case R.id.tvLingshi:
                changeMenu(2);
                break;
        }
    }

    public void changeMenu(int num) {
        tvZhuangbei.setTextSize(16);
        tvDaoju.setTextSize(16);
        tvLingshi.setTextSize(16);

        switch (num) {
            case 0:
                tvZhuangbei.setTextSize(22);
                if (bagGriveViewAdapter!=null){
                    bagGriveViewAdapter.setNum(0);
                }
                break;
            case 1:
                tvDaoju.setTextSize(22);
                if (bagGriveViewAdapter!=null){
                    bagGriveViewAdapter.setNum(1);
                }
                break;
            case 2:
                tvLingshi.setTextSize(22);
                if (bagGriveViewAdapter!=null){
                    bagGriveViewAdapter.setNum(2);
                }
                break;

        }
        if (bagGriveViewAdapter!=null){
            bagGriveViewAdapter.notifyDataSetChanged();
        }

    }

    private void getBagData(String usernumber) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + usernumber + "&need=beibao", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("beibao1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("beibao2", src);
                try {
                    userBag = new Gson().fromJson(src, UserBag.class);
                }catch (Exception e){
                    userBag=null;
                }
                Message message = new Message();
                message.obj = userBag;
                message.what=1;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            iv.setVisibility(View.INVISIBLE);
            animationDrawable.stop();
            switch (message.what){
                case 1:
                    userBag = (UserBag) message.obj;
                    if (null!=userBag){
                        bagGriveViewAdapter = new BagGriveViewAdapter(Bag.this, userBag);
                        gvBag.setAdapter(bagGriveViewAdapter);
                        tvZhuangbei.setOnClickListener(Bag.this);
                        tvDaoju.setOnClickListener(Bag.this);
                        tvLingshi.setOnClickListener(Bag.this);
                    }else{
                        ToastUtil.toast(Bag.this,"服务器坏掉了。。。");
                        tvZhuangbei.setOnClickListener(Bag.this);
                        tvDaoju.setOnClickListener(Bag.this);
                        tvLingshi.setOnClickListener(Bag.this);
                    }
                    break;
            }
            return false;
        }
    });


    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
        EventBus.getDefault().unregister(this);
        if (animationDrawable.isRunning()){
            animationDrawable.stop();
        }
        iv.clearAnimation();
    }

    /**
     * 接收后处理
     *
     * @param messageEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void shuaxing(MessageEvent messageEvent) {
        if (messageEvent.getMessage() == "Bag") {
            bagGriveViewAdapter.notifyDataSetChanged();
        }
    }
}
